﻿using System.Threading.Tasks;

namespace Sidvall.Net.Mail
{
    public class Attachment
    {
        #region Public Members

        public string FileName { get; set; }
        public string Name { get; set; }
        public string Content { get; set; }
        public System.IO.Stream ContentStream { get; set; }
        public AttachmentType AttachmentType { get; set; }

        #region GetBase64ContentAsync

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")]
        public async Task<string> GetBase64ContentAsync()
        {
            byte[] attachmentData;

            switch (this.AttachmentType)
            {
                case AttachmentType.File:
                    attachmentData = await Sidvall.SystemContext.Current.SystemStorageManager.ReadAllBytesAsync(this.FileName).ConfigureAwait(false);
                    return System.Convert.ToBase64String(attachmentData);
                case AttachmentType.Stream:
                    attachmentData = await Sidvall.SystemContext.Current.SystemStorageManager.ReadAllBytesAsync(this.ContentStream).ConfigureAwait(false);
                    return System.Convert.ToBase64String(attachmentData);
                case AttachmentType.String:
                    attachmentData = System.Text.Encoding.UTF8.GetBytes(this.Content);
                    return System.Convert.ToBase64String(attachmentData);
                default:
                    return null;
            }
        }

        #endregion

        #endregion
    }
}
